<template>
	<view class="container">
		<swiper class="banner-swiper" circular autoplay :interval="5000" :duration="1000">
			<swiper-item class="banner-swiper-item" v-for="(item, index) in swipers" :key="index">
				<image :src="item" mode="widthFix"></image>
			</swiper-item>
		</swiper>
		<view class="content">
			<!-- section-1 begin -->
			<view class="section-1">
				<navigator @tap="notify" class="item"   hover-class="none">
					<image src="/static/images/home/home_icon_ziqu1.png" mode="widthFix"></image>
					<view class="wenyue-font">门店自取</view>
					<view class="text-color-assist">下单免排队</view>
				</navigator>
				<navigator  class="item" open-type="switchTab" url="/pages/index/index" hover-class="none">
					<image src="/static/images/home/home_icon_waimai1.png" mode="widthFix"></image>
					<view class="wenyue-font">送货上门</view>
					<view class="text-color-assist">无需接触 送纸到家</view>
				</navigator>
			</view>
			<!-- section-2 end -->
			<!-- section-2 begin -->
			<view class="section-2">
				<navigator class="item" open-type="switchTab" url="" hover-class="none">
					<view class="title">
						<image src="/static/images/home/home_icon_baihuo1.png"></image>
						<view>雨瑞商贸</view>
					</view>
					<view class="tips">灵感商品 立即选购</view>
				</navigator>
				<navigator class="item" open-type="navigate" url="/pages/home/group-dinner" hover-class="none">
					<view class="title">
						<image src="/static/images/home/home_icon_qiye1.png"></image>
						<view>商家批发</view>
					</view>
					<view class="tips">童叟无欺,质量保证</view>
				</navigator>
			</view>
			<!-- section-2 end -->
			<view class="section-3">
				<navigator class="my-integral" open-type="navigate" url="" hover-class="none">
					<view class="integrals">
						<view>我的积分</view>
						<view class="neutra-font">{{userInfo.points}}</view>
					</view>
					<view class="tips">
						可使用积分兑换商品
					</view>
				</navigator>
				<navigator class="my-code" open-type="navigate" url="/pages/my/code" hover-class="none">
					<image src="/static/images/home/home_icon_erweima.png"></image>
					<view>会员码</view>
				</navigator>
			</view>
		</view>
		<!-- 登录popup -->
		<login-popup ref="loginPopup"></login-popup>
	</view>
</template>

<script>
	import { mapMutations,mapState } from 'vuex'
	import loginPopup from '../my/components/login-popup.vue'
	export default {
		components:{
			loginPopup
		},
		computed: {
			...mapState(['isLogin', 'userInfo','isAuth','swipers'])
		},
		async onLoad(){
			this.getUserInfo();
			
		},
		async onPullDownRefresh (){
			let that=this;
			let uid=uni.getStorageSync('userinfo').uid;
			that.getAddress(uid);
			 that.getCarousels();
			 that.getOrders();
			uni.stopPullDownRefresh()
		},
		methods: {
			async getCarousels(){
				let that=this;
				 that.$u.api.carousels().then(res=>{
					that.SET_CARSOUSEL(res.map(data=>data.imgUrl))
				})
			},
			 getAddress(uid){
				 let that=this;
			 this.$u.api.address.get(uid).then(res=>{
				 let resArray=[];
				 if(!that.$u.test.isEmpty(res)){
					 resArray=res;
				 }
				 that.SET_ADDRESS(resArray)
			 })
			},
			...mapMutations(['SET_USERINFO', 'SET_ISLOGIN','SET_AUTH','SET_TOKEN','SET_ADDRESS','SET_ORDER','SET_CARSOUSEL']),
			notify(){
				uni.showModal({
						title: '提示',
						content: '线下门店尚未开放,感谢关注雨瑞商贸',
				 		showCancel: false
				 	})
			},
			auth(){
				let that=this;
				return new Promise((reslove,reject)=>{
					if(!that.isAuth){
						uni.getSetting({
													      success: data => {
															  //判断是否授权
													        if (data.authSetting['scope.userInfo']) {
																that.SET_AUTH(true)
																reslove({});
													        } else {//null或者是false,表示没有授权，请求授权
															uni.authorize({
																scope:'scope.userInfo',
																success:res=>{
																	that.SET_AUTH(true);
																	reslove({});
																},
																fail:err=>{
																	that.SET_AUTH(false)
																	console.log('授权失败')
															
																	 reject();
																}
																,complete: () => {
																	
																}
															})
													        
													        }
													      },
														  fail: function(error) {
															  that.SET_AUTH(false)
															console.log(error);
														  }
													    })
					}else{
						reslove({});
					}
					
				})
			},
			async getOrders() {
				let that=this;
				that.$u.api.orders.get().then(res=>{
					that.SET_ORDER(res);
				})
				
			},
			async getUserInfo() {
				let that=this;
				 this.auth().then(res=>{
					uni.login({
						success(login_res) {
							if(login_res.errMsg==='login:ok'){
								
								uni.getUserInfo({
									success:userinfo=>{
										if(userinfo){
											//执行登录逻辑
											let req={
												code: login_res.code, //临时登录凭证
																			rowData: userinfo.rawData, //用户非
																			signature: userinfo.signature, //签名
																			encryptData: userinfo.encryptedData, 
																			iv: userinfo.iv //解密算法的向量
											}
											that.$u.api.login(req).then(check=>{
												that.SET_ISLOGIN(true)
												let user={...userinfo.userInfo,... check};
											
												that.SET_USERINFO(user);
												uni.setStorageSync('userinfo',user)
												that.getAddress(check.uid);
												 that.getCarousels();
												 that.getOrders();
												
												
											}).catch(err=>{
											
												that.SET_ISLOGIN(false)
												console.log(err)
											})
										}else{
											uni.showModal({
													title: '提示',
													content: '获取用户信息失败',
											 		showCancel: false
											 	})
												that.SET_ISLOGIN(false)
												return;
										}
									
									}
								})
							}else{
								uni.showModal({
										title: '提示',
										content: '登录失败,请稍后重试',
								 		showCancel: false
								 	})
									that.SET_ISLOGIN(false)
									return;
							}
							
							
							
						},
					
					})
				 }).catch(err=>{
					 this.$refs['loginPopup'].open();
					 // uni.showModal({
					 // 		title: '提示',
					 // 		content: '您已取消了授权，请重新授权登录',
					 //  		showCancel: false
					 //  	})
						// return;
				 })
				
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		max-height: 100%;
	}
	
	.banner-swiper {
		width: 100%;
		height: 600rpx;
		
		.banner-swiper-item {
			image {
				width: 100%;
			}
		}
	}
	
	.content {
		width: 100%;
		padding: 0 30rpx;
		position: relative;
	}
	
	.section-1 {
		position: relative;
		background-color: $bg-color-white;
		margin-top: -60rpx;
		border-radius: $border-radius-lg;
		padding: 60rpx 0;
		display: flex;
		margin-bottom: 30rpx;
		box-shadow: $box-shadow;
		
		.item {
			flex: 1;
			flex-shrink: 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			position: relative;
			
			&:nth-child(1):after {
				content: '';
				position: absolute;
				right: 0;
				top: 0;
				bottom: 0;
				width: 2rpx;
				background-color: $border-color;
			}
			
			image {
				width: 100rpx;
				margin-bottom: 20rpx;
			}
			
			.wenyue-font {
				font-size: 48rpx;
				margin-bottom: 10rpx;
			}	
		}
	}
	
	.section-2 {
		display: flex;
		justify-content: space-between;
		margin-bottom: 30rpx;
		
		.item {
			width: 335rpx;
			background-color: #EAEBEC;
			padding: $spacing-row-lg 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			border-radius: $border-radius-lg;
			
			.title {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: $font-size-lg;
				
				image {
					width: 60rpx;
					height: 60rpx;
					margin-right: 10rpx;
				}
			}
			
			.tips {
				color: $text-color-assist;
				font-size: $font-size-base;
			}
		}
	}
	
	.section-3 {
		margin-bottom: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: $font-size-base;
		color: $text-color-assist;
		padding: 0 10rpx;
		
		.my-integral {
			flex: 1;
			display: flex;
			flex-direction: column;
			
			.integrals {
				display: flex;
				align-items: center;
				font-size: $font-size-lg;
				color: $text-color-base;
				margin-bottom: 10rpx;
				
				.neutra-font {
					margin-left: 10rpx;
					font-size: 42rpx;
				}
			}
		}
		
		.my-code {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 0 30rpx;
			position: relative;
			
			image {
				width: 60rpx;
				height: 60rpx;
				margin-bottom: $spacing-col-sm;
			}
			
			&:before {
				content: " ";
				position: absolute;
				left: 0;
				top: 0;
				bottom: 0;
				border-left: 1rpx solid rgba($color: $border-color, $alpha: 0.6);
			}
		}
	}
</style>
